﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Drawing.Imaging;
using System.Collections;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using androidpost.Utils;



namespace androidpost
{
    public partial class ControlPPT : Form
    {
        string butKHText1 = "Stop"; //"Dừng lại"
        string kichhoat = "Start";//"Kích hoạt"
        string ketnoistr = "Connect";//"Kết nối"
        int aa=0,ketnoi;
        int control;
        int check=0;
        int dem = 0;
        Thread t;
        String res;
        delegate void SetTextCallback(string text);
        public ControlPPT()
        {
            InitializeComponent();
        }
        WebClient cGet = new WebClient();
        private void button1_Click(object sender, EventArgs e)
        {
            


            lbldem.Text = "" + dem;
            if (butKichHoat.Text != butKHText1)
            {
                txtKichHoat.Text =kichhoat+ ":  Enable";
                butKichHoat.Text = butKHText1;
                cGet.CancelAsync();
                res = cGet.DownloadString(Constant.domain + "postandroid.php?res=00000tt" + Constant.id);
                ketnoi = 1;
                if (t != null) t.Abort();
                   this.t = new Thread(new ThreadStart(this.startConnect)); this.t.Start();
                   
                
            }
            else
            {
                butKichHoat.Text = kichhoat;
                txtKichHoat.Text = kichhoat+":  Disable";
                ketnoi = 0;
                txtKetNoi.Text = ketnoistr+": Stoped";
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ketnoi = 0;
            control = 0;
            
        }

        private void startConnect()
        {

            
            int p = 0;
            String ab = "----------";
            String so="";
            String so1, so2;
            int newc = 1;
            do
            {
                dem++;
                p++;
               
                try
                {
                    Thread.Sleep(Utils.Constant.REQUEST_TIMEOUT);
                   
                  
                    //if(so.Length>13)so = so.Substring(3);
                   
                    so1 = so.Substring(0, 5);
                    newc = Int32.Parse(so1);
                    so2 = so.Substring(5, 2);
                    control = Int32.Parse(so2);
                    
                 if(ketnoi!=0)   this.SetText(ketnoistr+": Connected");
                }
                catch (Exception)
                {
                    
                    control = 0;
                    if (aa < 6)
                    {

                        if (ketnoi != 0) this.SetText(ketnoistr+": Connecting." + ab.Substring(0, aa));
                       
                        aa++;
                    }
                    else aa = 0;
                }

                try
                {
                   // so = cGet.DownloadString(Constant.domain + "chung.php?id=" + Constant.id);

                    so = Network.NetworkSupport.methodPost(Constant.CONTENTTYPE_JSON, null, Constant.domain + "chung.php?id=" + Constant.id, "");
                    
                   
                }
                catch (Exception)
                {

                    this.SetText(ketnoistr+": Lose connection");
                    dem--;
                }
                    


               // Log2file("" + newc+control);
                

                if (newc > check)
                {
                    
                    sendppt a = new sendppt(control);
                   
                }
                check = newc;
              
                
            } while (ketnoi==1);
            
            
        }
        
        private void SetText(string text) // cái này để settex cho cái txtketnoi. vì có thêm thread nên fai thế này
        {
            
            if (this.txtKetNoi.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                try
                {
                    this.Invoke(d, new object[] { text });
                }
                catch (Exception)
                {
                    
                    
                }
                
            }
            else
            {
                this.txtKetNoi.Text = text;
                this.lbldem.Text = "" + dem;
                
            }
        }

        int pro1=0;
        private void timer1_Tick(object sender, EventArgs e)
        {
           
           float ss2 = dem - pro1;
            int ss = (int)((ss2 / 10) * 100);
           // lblpercent.Invoke((Action)(() => lblpercent.Text = ""+ss+"%"));
            lblpercent.Text = "" + ss + "%";
            if (ss > 100&ketnoi==1) { progressBar1.Value = 99; }
            else progressBar1.Value = ss;
            progressBar1.PerformStep();
            pro1 = dem;

        }

       

        private void notifyIcon1_Click(object sender, EventArgs e)
        {
            this.Show();
            
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            ketnoi = 0;
            try
            {
                Environment.Exit(1);
            }
            catch (Exception)
            {
                
                throw;
            }

            Application.ExitThread();
            Application.Exit();
            
        }

        

       
        private void lblAbout_Click(object sender, EventArgs e)
        {
            
            MessageBox.Show("Easy Remote PPT ver1.0 \n \nMade by: Nguyễn Anh Quế \n Phone: +84977310197 \n Mail: 2@naq.name.vn ");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.ExitThread();
            
        }


        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void butHide_Click(object sender, EventArgs e)
        {
           
        }


    }
}
